/*
 * 选择条件的生成, 品牌、类别、价格
 */
var Condition = {
	/**
	 * 保存品牌数据
	 */
	brands: [],
	/**
	 * 保存类别数据
	 */
	types: [],
	/**
	 * 价格区间
	 */
	prices: [
		{
			name: "1000元以下",
			min: 0,
			max: 1000
		},
		{
			name: "1000-5000元",
			min: 1001,
			max: 5000
		},
		{
			name: "5000元以上",
			min: 5001,
			max: Infinity
		}
	]
};

/*
 * 初始化的时候获取品牌及类别
 */
Condition.init = function() {	
	//获取品牌
	jQuery.getJSON(
		"api/v1/getAllBrands.json",
		function(json) {
			if(json.success) {
				Condition.brands = json.result;
			}
		}
	);
	//获取类别
	jQuery.getJSON(
		"api/v1/getAllTypes.json",
		function(json) {
			if(json.success) {
				Condition.types = json;
			}
		}
	);
	/*
	//选择品牌
	jQuery('.condition-box').on("click", "li.choose-brands", function() {
		var me = jQuery(this);
		jQuery("#select_brand_name")
		.html(me.attr("data-name"))
		.attr("data-id", me.attr("data-id"));
		
		offCanvasWrapper.offCanvas('show');
	});
	
	//选择类别
	jQuery('.condition-box').on("click", "li.choose-type", function() {
		var me = jQuery(this);
		jQuery("#select_brand_type")
		.html(me.attr("data-name"))
		.attr("data-id", me.attr("data-id"));
		offCanvasWrapper.offCanvas('show');
	});*/
	
	mui(".condition-box").on("tap", "li.choose-brands", function(e){
		var me = jQuery(e.target);
		var id = me.attr("data-id");
		
		var span = jQuery("#select_brand_name").attr("data-id", id);
		if( id !== "-1") {
			span.html(me.attr("data-name"))
		}
		else {
			span.html("");
		}
		
		offCanvasWrapper.offCanvas('show');
	});
	
	mui(".condition-box").on("tap", "li.choose-type", function(e){
		var me = jQuery(e.target);
		var id = me.attr("data-id");
		
		var span = jQuery("#select_type_name").attr("data-id", id);
		if( id !== "-1") {
			span.html(me.attr("data-name"))
		}
		else {
			span.html("");
		}
		
		offCanvasWrapper.offCanvas('show');
	});
	
	mui(".condition-box").on("tap", "li.choose-price", function(e){
		var me = jQuery(e.target);
		var id = me.attr("data-id");
		
		var span = jQuery("#select_price_name").attr({
			"data-min": me.attr("data-min"),
			"data-max": me.attr("data-max")
		});
		if( id !== "-1") {
			span.html(me.attr("data-name"))
		}
		else {
			span.html("");
		}
		
		offCanvasWrapper.offCanvas('show');
	});
}

/**
 * 显示品牌
 */
Condition.showBrands = function() {
	jQuery(".condition-head-title").html("品牌");
	var box = jQuery(".condition-box").html("");
	var data = Condition.brands;
	var h = '<li class="mui-table-view-cell choose-brands" data-id="-1">不限</li>';
	for(var i=0, len=data.length; i<len; i++) {
		var item = data[i];
		h += '<li class="mui-table-view-cell choose-brands"'
		   + ' data-id="'+ item.id +'" data-name="'+ item.name +'">'
		   + item.name
		   + '</li>';
	}	
	box.html(h);
}

/**
 * 显示类别
 */
Condition.showTypes = function() {
	jQuery(".condition-head-title").html("类别");
	var box = jQuery(".condition-box").html("");
	var e = Condition.types;
	var data = e.result;
	var h = '<li class="mui-table-view-cell choose-type" data-id="-1">不限</li>';
	for(var i=0, len=data.length; i<len; i++) {
		var item = data[i];
		if(item.kids && item.kids.length > 0) {
			h += '<li class="mui-table-view-cell mui-collapse" >'
		   	   + '	<a class="mui-navigate-right" href="#">'+ item.name +'</a>'
		   	   + '	<ul class="mui-table-view mui-table-view-chevron">';
		   	for(var j=0,length=item.kids.length; j<length; j++) {
		   		var obj = item.kids[j];
		   		h += '	<li class="mui-table-view-cell choose-type"'
		   		   + ' data-id="'+ obj.id +'" data-name="'+ obj.name +'">'
//		   		   + '		<a class="mui-navigate-right" href="#">'+obj.name+'</a>'
		   		   + obj.name
		   		   + '	</li>';
		   	}
		   	
		   	h += '	</ul>';
						
		}
		else {
			h += '<li class="mui-table-view-cell choose-type" '
		       + ' data-id="'+ item.id +'" data-name="'+ item.name +'">';
			   + item.name;
		}
		
		h  += '</li>';
	}	
	box.html(h);
}

/**
 * 显示价格区间选择
 */
Condition.showPrices = function() {
	jQuery(".condition-head-title").html("价格");
	var box = jQuery(".condition-box").html("");
	var data = Condition.prices;
	var h = '<li class="mui-table-view-cell choose-price" data-id="-1">不限</li>';
	for(var i=0, len=data.length; i<len; i++) {
		var item = data[i];
		h += '<li class="mui-table-view-cell choose-price" '
		   + ' data-min="'+ item.min + ' data-max="'+ item.max +'"' +'" data-name="'+ item.name +'">'
		   + item.name
		   + '</li>';
	}	
	box.html(h);
}

















